<template>
	<!-- 权限系统-角色管理 -->
	<d2-container class="d-flex flex-column mod-sys__user">
		<template v-if="$hasPermission('delivery:promotionLink:index')">
			<div class="d-flex h-100 b-s flex-column">
				<el-form class="d-flex b-s mt-2 mb-10" :inline="true" size="small" :model="dataForm"
					@keyup.enter.native="getTableList(true)" @submit.native.prevent>
					<el-form-item label="名称">
						<el-input v-model="dataForm.keyword" placeholder="名称" clearable />
					</el-form-item>
					<el-form-item>
						<el-button icon="el-icon-search" type="primary"
							@click="getTableList(true)">查询</el-button>
					</el-form-item>
				</el-form>
				<div class="d-flex flex-1">
					<div class="overflow">
						<vxe-grid keep-source border resizable highlight-hover-row size="small" ref="pGrid" :columns="columns" height="100%"
							:loading="dataListLoading" :data="dataList" :customs.sync="customColumns"
							:edit-config="{trigger: 'click', mode: 'row', showStatus: true}">
						</vxe-grid>
					</div>
				</div>
			</div>
			<!-- 分页 -->
			<div class="d-flex" slot="footer">
				<vxe-pager background size="medium" align="right" :border="false"
					:current-page="dataForm.page" :page-size="dataForm.pageSize" :total="total"  :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'Sizes', 'FullJump', 'Total']"
					@page-change="handlePageChange">
				</vxe-pager>
			</div>
			<!-- 新增/修改弹窗 -->
			<promotionLink-add-or-update v-if="addOrUpdateVisible" ref="addOrUpdateDialog" @refreshDataList="getTableList"></promotionLink-add-or-update>
		</template>
		<template v-else>
			<div class="d-flex a-center j-center h-100 b-s flex-column">
				<img class="d-flex b-s mt-2 mb-10" src="../../../public/image/download.png" alt="没有访问权限" style="width: 300px;">
				<div class="d-flex">抱歉，您没有访问权限</div>
			</div>
		</template>
	</d2-container>
</template>

<script>
	import mixinViewModule from '@/mixins/view-module'
	import PromotionLinkAddOrUpdate from './promotionLink-add-or-update'
	export default {
		mixins: [mixinViewModule],
		components: {
			PromotionLinkAddOrUpdate
		},
		data() {
			return {
				
				total:0,
				curName:'',
				// 增改
				addOrUpdateVisible: false,
				roleVisible:false,
				dataList:[],
				dataForm: {
					page:1,
					pageSize:10,
					keyword: ''
				},
				customColumns: [],
				dataFormOp: {
					roleName: 'like'
				},
				columns: [{
						title: 'ID',
						field: 'id',
						width: '20%',
						sortable: true,
						align: 'center'
					},
					{
						title: '公司名称',
						field: 'name',
						width: '20%',
						sortable: false,
						align: 'center'
					},
					{
						title: '公司绑定的小程序名称',
						field: 'applet_name',
						width: '20%',
						sortable: false,
						align: 'center'
					},
					{
						title: '状态',
						field: 'status_name',
						width: '20%',
						sortable: false,
						align: 'center'

					},
					{
						title: '创建时间',
						field: 'created_at',
						width: '20%',
						sortable: true,
						align: 'center'
					},
					{
						title: '更新时间',
						field: 'updated_at',
						width: '20%',
						sortable: true,
						align: 'center'
					},
					{
						title: '操作',
						field: 'other',
						width: 290,
						fixed:'right',
						sortable: false,
						align: 'center',
						slots: {
							default: ({
								row
							}) => {
								return [ 
									<el-button size = "mini" round
										icon = "el-icon-edit"
										v-show={this.$hasPermission('delivery:promotionLink:update')}
										onClick = {
											() => this.addOrUpdateData(row)
										}
										type = "primary" > 修改 
									</el-button>, 
									<el-button size = "mini" round
									icon = "el-icon-delete"
									v-show={this.$hasPermission('delivery:promotionLink:delete')}
									type = "danger"
									onClick = {
										() => this.deleteHandleSetter(row)
									} > 删除 </el-button>, 
								]
							}
						}
					}

				]
			}
		},
		created() {
			if(this.$hasPermission('delivery:promotionLink:index')){
				this.getTableList();
			}
		},
		methods: {
			async getTableList(init=false){
				if(init&&typeof init =='boolean'){
					this.dataForm.page = 1;
				}
				const tableRes = await this.$axios.get('/api/delivery/company?'+this.$qs.stringify(this.dataForm));
				if(typeof tableRes == 'object' && tableRes!=null){
					this.total = tableRes.total||0;
					if(Array.isArray(tableRes.data)){
						let tdlen = tableRes.data.length;
						if(tdlen){
							this.dataList = tableRes.data;
							return
						}
					}
				}
				this.dataList = [];
			},
			//新增/修改弹窗
			async addOrUpdateData(row) {
				console.log(row)
				this.addOrUpdateVisible = true;
				if (typeof row == 'object' && row!=null) {
					// this.curName = row.real_name;
					const id = row.id;
					const userRes = await this.$axios.get(`/api/delivery/company/update/${id}`,{});
					this.$nextTick(() => {
						this.$refs.addOrUpdateDialog.userId = id;
						this.$refs.addOrUpdateDialog.update(userRes);
					})
				} else {
					this.$nextTick(() => {
						this.$refs.addOrUpdateDialog.userId = null;
						this.$refs.addOrUpdateDialog.init()
					})
				}
			},
			// 删除
			deleteHandleSetter(row) {
				this.$confirm(this.$t('public.prompt.info', {
					handle: this.$t('views.public.delete')
				}), this.$t('public.prompt.title'), {
					confirmButtonText: this.$t('views.public.confirm'),
					cancelButtonText: this.$t('views.public.cancel'),
					type: 'warning'
				}).then(() => {
					this.$axios.delete(`/api/delivery/company/delete/${row.id}`,{}
					).then(res => {
						this.$message({
							message: this.$t('views.public.success'),
							type: 'success',
							duration: 500,
							onClose: () => {
								this.getTableList()
							}
						})
					}).catch(() => {})
				}).catch(() => {})
			}
		}
	}
</script>

<style>

</style>